package com.my.study.cas;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * ABA问题解决:
 * 1、修改的时候修改版本号(类似时间戳)
 */
public class ABADemo {

    // 不带版本
    private static AtomicReference<Integer> userAtomicReference = new AtomicReference(100);
    // 带版本号
    private static AtomicStampedReference<Integer> stampedReference = new AtomicStampedReference(100, 1);


    /**
     * 原子引用对象
     *
     * @param args
     */
    public static void main(String[] args) {
        // testABAProblem();
        handlerABAProblem();
    }

    private static void handlerABAProblem() {
        System.out.println("============以下是t3解决ABA问题=============");
        // 线程暂停2秒钟,t4获取到未修改前的版本号
        try {
            TimeUnit.SECONDS.sleep(2);
            new Thread(() -> {
                int stamp = stampedReference.getStamp();
                System.out.println("==============t3第1次版本号" + stamp);
                boolean t3Result = stampedReference.compareAndSet(100, 101, stamp, stamp + 1);
                System.out.println("==============t3第1次执行结果:" + t3Result + " " + stampedReference.getReference());
                System.out.println("==============t3第2次版本号" + stampedReference.getStamp());
                boolean t3Result2 = stampedReference.compareAndSet(101, 100,
                        stampedReference.getStamp(), stampedReference.getStamp() + 1);
                System.out.println("==============t3第二次执行完成,执行结果:" + t3Result2 + ",最后结果:" + stampedReference.getReference());
            }, "t3").start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        new Thread(() -> {
            try {
                int stamp = stampedReference.getStamp();
                System.out.println("==============t4第1次版本号" + stamp);
                // 暂停2秒让t3完成版本号修改
                TimeUnit.SECONDS.sleep(2);
                // 第一次执行(复现ABA,结果执行失败)
                boolean compareAndSet = stampedReference.compareAndSet(100, 101, stamp, stamp + 1);
                System.out.println("==============t4第一次执行结果:" + compareAndSet + ",当前版本号:" + stamp + ",实际版本号:" + stampedReference.getStamp());
                // 第二次执行(解决ABA)
                System.out.println("==============t4第二次版本号:" + stampedReference.getStamp());
                System.out.println("==============t4第二次执行结果:" + stampedReference
                        .compareAndSet(100, 101, stampedReference.getStamp(), stampedReference.getStamp() + 1)
                        + ",结果为" + stampedReference.getReference());
                System.out.println("==============t4第二次执行后,最新版本号为:" + stampedReference.getStamp());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t4").start();
    }

    /**
     * 导致ABA问题
     */
    private static void testABAProblem() {
        // 原子引用对象
        new Thread(() -> {
            userAtomicReference.compareAndSet(100, 101);
            userAtomicReference.compareAndSet(101, 100);
            System.out.println(Thread.currentThread().getName() + "完成了ABA问题");
        }, "t1").start();

        new Thread(() -> {
            // 线程执行顺序与代码前后无关,t2必须等待2秒中,t1完成了ABA问题
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(userAtomicReference.compareAndSet(100, 2000) + "\t " + userAtomicReference.get());
        }, "t2").start();
    }

}
